#include "PrecompiledHeader.h"
#include "CHud.h"

/*--------------------------------------------------------------------------*/
CHud* g_Hud = NULL;
/*--------------------------------------------------------------------------*/

CHud::CHud() :
	IEngMod("CHud"),
	IRender(ERenderFlag::None),
	m_doScreenshot(false)
{
	AssertModule(g_Shell);
	g_Shell->RegisterLuaFunction("guiadd", sh::guiadd);
	this->var_forms_enabled = g_Shell->AddVar("hud_enabled", 1);

	this->StartTask(0, CalcDesc(true, ECalc::MultiThreadHighPriority));

	/*--------------------------------------------------------------------------*/

	RenderTaskDesc desc;
	desc.m_material = NULL;
	desc.m_argument = 0;
	desc.m_flags = ERenderTaskFlag::UserInterface | ERenderTaskFlag::NoMaterial;
	desc.m_priority = 99;
	desc.m_object = this;

	this->AddRenderTask(desc);

	/*--------------------------------------------------------------------------*/

	this->m_statisticsHud	= new StatisticsHud();
	this->m_consoleHud		= new ConsoleHud();
	this->m_guiHud			= new GuiHud();
}

/*--------------------------------------------------------------------------*/

CHud::~CHud()
{
	delete this->m_statisticsHud;
	delete this->m_consoleHud;
	delete this->m_guiHud;
}

/*--------------------------------------------------------------------------*/

void CHud::OnCalc(uint arg)
{
	switch (arg)
	{
	case 0:
	{
		if (g_Keyboard->GetKeyState(EKey::k_f1))
		{
			this->m_doScreenshot = true;
			g_Keyboard->SetKeyState(EKey::k_lshift, false);
		}
		else if (this->m_doScreenshot)
		{
			g_Shell->Execute("screenshot()");
			this->m_doScreenshot = false;
		}
		else
		{
			this->m_guiHud->OnHudUpdate();
			this->m_consoleHud->OnHudUpdate();
			this->m_statisticsHud->OnHudUpdate();

			this->BindRenderTasks(g_Renderer->GetUIRenderPhase());
		}

		break;
	}
	}
}

/*--------------------------------------------------------------------------*/

void CHud::OnRender(const RenderTask* renderTask, uint camarg)
{
	switch (renderTask->GetArgument())
	{
	case 0:
	{
		this->m_guiHud->OnHudRender();
		this->m_statisticsHud->OnHudRender();
		this->m_consoleHud->OnHudRender();

		break;
	}
	}
}

/*--------------------------------------------------------------------------*/

ConsoleHud* CHud::GetConsoleManager()
{
	return this->m_consoleHud;
}

/*--------------------------------------------------------------------------*/

StatisticsHud* CHud::GetStatisticsManager()
{
	return this->m_statisticsHud;
}

/*--------------------------------------------------------------------------*/

GuiHud* CHud::GetGuiManager()
{
	return this->m_guiHud;
}

/*--------------------------------------------------------------------------*/

void sh::guiadd(const std::string& name)
{
	AssertModule(g_Hud);

	Gui* gui = NULL;
	g_Resources->Get(name, &gui);

	g_Hud->GetGuiManager()->AddGui(gui);
}

/*--------------------------------------------------------------------------*/